using System;
using System.Diagnostics;
using System.Management.Automation;             //Windows PowerShell namespace.
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using VirtualOps.Base;

namespace VmWareVix.Commands
{

  public class VmCommandBase : Cmdlet
  {
    [Parameter(Position = 0, ValueFromPipeline = true)]
    [ValidateNotNullOrEmpty]
    public IVirtualMachine[] VMs
    {
      get { return _vms; }
      set { _vms = value; }
    }
    protected IVirtualMachine[] _vms;

    protected override void ProcessRecord()
    {
      foreach (IVirtualMachine vm in _vms)
      {
        VmCommand(vm);
      }
      WriteObject(_vms, false);
    }

    protected virtual void VmCommand(IVirtualMachine vm) {}

  }

  [Cmdlet(VerbsLifecycle.Suspend, "VM")]
  public class SuspendVmCommand : VmCommandBase
  {
    protected override void VmCommand(IVirtualMachine vm)
    {
      WriteVerbose(String.Format("Suspending VM {0}...", vm.Name));
      vm.Suspend();
      WriteVerbose(String.Format("VM {0} suspended", vm.Name));
    }
  }
  
  [Cmdlet(VerbsLifecycle.Resume, "VM")]
  public class ResumeVmCommand : VmCommandBase
  {
    protected override void VmCommand(IVirtualMachine vm)
    {
      WriteVerbose(String.Format("Resuming VM {0}...", vm.Name));
      vm.Resume();
      WriteVerbose(String.Format("VM {0} resumed", vm.Name));
    }
  }
  
  [Cmdlet(VerbsLifecycle.Start, "VM")]
  public class StartVmCommand : VmCommandBase
  {
    protected override void VmCommand(IVirtualMachine vm)
    {
      WriteVerbose(String.Format("Starting VM {0}...", vm.Name));
      vm.Start();
      WriteVerbose(String.Format("VM {0} started", vm.Name));
    }
  }

  [Cmdlet(VerbsLifecycle.Stop, "VM")]
  public class StopVmCommand : VmCommandBase
  {
    protected override void VmCommand(IVirtualMachine vm)
    {
      WriteVerbose(String.Format("Stopping VM {0}...", vm.Name));
      vm.Stop();
      WriteVerbose(String.Format("VM {0} stopped", vm.Name));
    }
  }

  [Cmdlet(VerbsCommon.Remove, "VM")]
  public class RemoveVmCommand : VmCommandBase
  {
    protected override void VmCommand(IVirtualMachine vm)
    {
      WriteVerbose(String.Format("Removing VM {0}...", vm.Name));
      vm.Remove();
      WriteVerbose(String.Format("VM {0} removed", vm.Name));
    }
  }

   
    
    //TODO: consider using object VmCommand() signature
    //where return parameter is what is written to the pipe
    //protected override void VmCommand(VirtualMachine vm)
  
}
